package cn.edu.njupt.post.controller;

import cn.edu.njupt.post.model.EntityType;
import cn.edu.njupt.post.model.HostHolder;
import cn.edu.njupt.post.model.Post;
import cn.edu.njupt.post.model.ViewObject;
import cn.edu.njupt.post.service.FollowService;
import cn.edu.njupt.post.service.LikeService;
import cn.edu.njupt.post.service.PostService;
import cn.edu.njupt.post.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    PostService postService;

    @Autowired
    UserService userService;

    @Autowired
    LikeService likeService;

    @Autowired
    FollowService followService;

    @Autowired
    HostHolder hostHolder;

    private List<ViewObject> getPosts(int userId, int offset, int limit) {
        //System.out.println(postService.getLatestPostsByUser(userId, offset, limit));
        List<Post> postList;
        if (userId == -1) postList = postService.getLatestPosts(offset, limit);
        else postList = postService.getLatestPostsByUser(userId, offset, limit);
        List<ViewObject> vos = new ArrayList<>();
        for (Post post : postList) {
            ViewObject vo = new ViewObject();
            vo.set("post", post);
            vo.set("user", userService.getUser(post.getUserId()));
            vo.set("likeCount", likeService.getLikeCount(EntityType.ENTITY_POST, post.getId()));
            vos.add(vo);
        }
        return vos;
    }

    @RequestMapping(path = {"/", "/index"}, method = {RequestMethod.GET})
    public String index(Model model,
                        @RequestParam(value = "pId", defaultValue = "1") int pId) {
        model.addAttribute("vos", getPosts(-1, (pId - 1) * 10, 10));
        model.addAttribute("hots", postService.getHotPosts(0, 5));
        int lastPage = postService.getPostCount();
        lastPage = lastPage / 10 + ((lastPage % 10 == 0) ? 0 : 1);
        if (0 < pId && pId < lastPage) model.addAttribute("nextPId", pId + 1);
        if (1 < pId && pId <= lastPage) model.addAttribute("prePId", pId - 1);
        return "index";
    }



    @RequestMapping(path = {"/user/{userId}", "/user/{userId}/index", "/user/{userId}/home"}, method = {RequestMethod.GET, RequestMethod.POST})
    public String userIndex(Model model, @PathVariable("userId") int userId,
                            @RequestParam(value = "pId", defaultValue = "1") int pId) {
        model.addAttribute("thisUser", userService.getUser(userId)); //user not found
        if (userService.getUser(userId) == null) {
            return "404";
        }
        if (hostHolder.getUser() == null) {
            ;
        } else if (followService.isFollower(hostHolder.getUser().getId(), EntityType.ENTITY_USER, userId)) model.addAttribute("followed", 1);
        model.addAttribute("followeeCount", followService.getFolloweeCount(userId, EntityType.ENTITY_USER));
        model.addAttribute("followerCount", followService.getFollowerCount(EntityType.ENTITY_USER, userId));
        model.addAttribute("vos", getPosts(userId, 10 * (pId - 1), 10));

        int lastPage = postService.getPostCountById(userId);
        lastPage = lastPage / 10 + ((lastPage % 10 == 0) ? 0 : 1);
        if (0 < pId && pId < lastPage) model.addAttribute("nextPId", pId + 1);
        if (1 < pId && pId <= lastPage) model.addAttribute("prePId", pId - 1);
        return "personal";
    }
}
